package client_commands;

import java.util.StringTokenizer;

import javax.swing.JOptionPane;

import client.ClientApplication;
import client.ClientSocketConnection;
import client_tree.User;

public class SERV_ADDEDCommand extends Command {
	
	public SERV_ADDEDCommand(String parameter) {
		setCode(SERV_ADDED);
		setParameter(parameter);
	}
	
	
	@Override
	public boolean executeCommand(ClientApplication theApp) {
		StringTokenizer tokens = new StringTokenizer(getParameter());
		
		String username = tokens.nextToken();
		
		String addingUser = tokens.nextToken();
		String nickname = tokens.nextToken();
		String email = tokens.nextToken();
		String ip_address = tokens.nextToken();
		String localAddress = tokens.nextToken();
		String port = tokens.nextToken();
		String status = tokens.nextToken();
		String regDate = tokens.nextToken();
		String avatar = tokens.nextToken();
		
		String message = "User \"" + addingUser + "\" added you in his contact list.\nDo you want to add him to your contact list?"; 
		
	    int action = JOptionPane.showConfirmDialog(theApp.getWindow(), message, "Add Info", JOptionPane.YES_NO_OPTION);

	    
	    ClientSocketConnection connection = theApp.retrieveSocketConnection();
	    
	    
	    if (action == JOptionPane.YES_OPTION) {
	    	StringBuffer addCommand = new StringBuffer();
	    	addCommand.append("ADD ");
	    	addCommand.append(username);
	    	addCommand.append(" ");
	    	addCommand.append(addingUser);
	    	
	    	String completedInfo = addCommand.toString();
	    	
	    	connection.setMessage(ADD_CMD, completedInfo);
	    	
			String response;
			
			response = connection.buffers[ADD_CMD].read();
			
			if ( response.startsWith("100") ) {
				User user = new User(addingUser, nickname, email, ip_address, localAddress, Integer.parseInt(port), status, regDate, avatar);
				theApp.getClientModel().addUser(user);
				
				StringBuffer buffer = new StringBuffer();
				
				buffer.append("CHSTATUS ");
				buffer.append(theApp.getClientUser().getUsername());
				buffer.append(" ");
				buffer.append(STATUS_NAMES[theApp.getClientUser().getStatus()]);
				buffer.append(" ");
				buffer.append(theApp.getClientUser().getNickname());
				buffer.append(" ");
				buffer.append(theApp.getClientUser().getEmail());
				buffer.append(" ");
				buffer.append(theApp.getClientUser().getAvatar());
				
				String completedStatusInfo = buffer.toString();
				
				connection.setMessage(CHSTATUS_CMD, completedStatusInfo);
			}
			else {
				String warningMessage = "User \" " + addingUser + "\" is not member of this beautiful messenger SEX!";
				JOptionPane.showMessageDialog(theApp.getWindow(), warningMessage, "Adding User", JOptionPane.WARNING_MESSAGE);
			}
	    	
	    	return true;
	    }
	    
	    
	    if (action == JOptionPane.NO_OPTION) {	
	    	StringBuffer blockCommand = new StringBuffer();
	    	blockCommand.append("BLOCK ");
	    	blockCommand.append(username);
	    	blockCommand.append(" ");
	    	blockCommand.append(addingUser);
	    	
	    	String completedInfo = blockCommand.toString();
	    	
	    	connection.setMessage(BLOCK_CMD, completedInfo);
	    	
			String response;
			
			response = connection.buffers[BLOCK_CMD].read();
			
			if ( response.startsWith("100") ) {
				theApp.getClientUser().addToBlockList(addingUser);
				theApp.getWindow().refreshTree();		
			}
	    	return true;
	    }
	    
		
		return false;
	}

}
